﻿using System;
using System.IO;
using System.Web;
using System.Web.UI;

namespace DreamCube.Foundation.Basic.Utility
{
    /// <summary>
    /// 操作网页page对象
    /// </summary>
    public static class MyPage
    {
        /// <summary>
        /// 获取当前网页所在的目录物理路径，返回的字符串最后不包含斜杠符号：“\\”
        /// </summary>
        public static String CurFolderPhysicalPath
        {
            get
            {
                return HttpContext.Current.Server.MapPath(".");
            }
        }

        /// <summary>
        /// 获取当前网页所在的目录的上层目录的物理路径，返回的字符串最后不包含斜杠：“\\”
        /// </summary>
        public static String ParentFolderPhysicalPath
        {
            get
            {
                String curpath = CurFolderPhysicalPath;
                return MyString.LeftOfLast(curpath, "\\");
            }
        }

        /// <summary>
        /// 获取当前网页所在的相对目录（相对web根目录），返回的字符串最后不包含斜杠“/”
        /// </summary>
        public static String CurFolderPath
        {
            get
            {
                String url = HttpContext.Current.Request.ServerVariables["URL"];
                return MyString.LeftOfLast(url, "/");
            }
        }

        /// <summary>
        /// 获取当前网页所在的目录的父目录，返回的字符串最后不包含斜杠“/”
        /// </summary>
        public static String ParentFolderPath
        {
            get
            {
                String url = HttpContext.Current.Request.ServerVariables["URL"];
                String parentFolder = MyString.LeftOfLast(url, "/", true, "");
                if (parentFolder.Length == 0) return "/";
                return MyString.LeftOfLast(parentFolder, "/");
            }
        }

        /// <summary>
        /// 当前网页所在目录的url路径，返回的字符串最后包含斜杠“/”
        /// </summary>
        public static String CurFolderUrl
        {
            get
            {
                String pathInfo = MyString.LeftOfLast(HttpContext.Current.Request.Path, "/") + "/";
                return MyString.LeftOfLast(MyWebsite.GetServerBasicAddr(), "/") + pathInfo;
            }
        }

        /// <summary>
        /// 当前网页的父目录url路径，返回的字符串最后包含斜杠“/”
        /// </summary>
        public static String ParentFolderUrl
        {
            get
            {
                String curUrl = CurFolderUrl;
                return MyString.LeftOfLast(MyString.LeftOfLast(curUrl, "/"), "/") + "/";
            }
        }

        /// <summary>
        /// 在客户端显示弹出对话框
        /// </summary>
        /// <param name="webPage"></param>
        /// <param name="msg">要显示的信息</param>
#if NET20
        public static void Alert(Page webPage, String msg)
#else
        public static void Alert(this Page webPage, String msg)
#endif
        {
            if (webPage != null)
            {
                webPage.ClientScript.RegisterStartupScript(webPage.GetType(), "alert", "alert('" + msg + "');", true);
            }
        }

        /// <summary>
        /// 在客户端执行一段脚本
        /// </summary>
        /// <param name="webPage"></param>
        /// <param name="js">要执行的命令</param>
#if NET20
        public static void ExecJs(Page webPage, String js)
#else
        public static void ExecJs(this Page webPage, String js)
#endif
        {
            if (webPage != null)
            {
                webPage.ClientScript.RegisterStartupScript(webPage.GetType(), "ExecJs", "<script language=\"javascript\">" + js + ";</script>");
            }
        }
    }
}
